package com.idormy.sms.forwarder.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.idormy.sms.forwarder.activity.SplashActivity
import com.idormy.sms.forwarder.utils.Log

@Suppress("PrivatePropertyName")
class BootCompletedReceiver : BroadcastReceiver() {

    private val TAG: String = BootCompletedReceiver::class.java.simpleName

    override fun onReceive(context: Context, intent: Intent?) {

        if (intent?.action != Intent.ACTION_BOOT_COMPLETED && intent?.action != Intent.ACTION_LOCKED_BOOT_COMPLETED) return

        try {
            Log.d(TAG, "开机自启动：仅启动后台服务，不启动UI界面")
            
            // 开机自启动时只启动前台服务，不启动UI
            // 这样不会触发密码验证，不影响后台任务运行
            val serviceIntent = Intent(context, com.idormy.sms.forwarder.service.ForegroundService::class.java)
            serviceIntent.action = com.idormy.sms.forwarder.utils.ACTION_START
            
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                context.startForegroundService(serviceIntent)
            } else {
                context.startService(serviceIntent)
            }
            
            Log.d(TAG, "后台服务启动成功")
        } catch (e: Exception) {
            e.printStackTrace()
            Log.e(TAG, "启动后台服务失败:${e.message}")
        }

    }
}